package com.atguigu.gmall.item.service.impl;

import com.alibaba.fastjson.JSON;
import com.atguigu.gmall.common.constant.RedisConst;
import com.atguigu.gmall.item.service.ItemService;
import com.atguigu.gmall.list.client.ListFeignClient;
import com.atguigu.gmall.model.product.*;
import com.atguigu.gmall.product.client.ProductFeignClient;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RBloomFilter;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.stream.Collectors;

/**
 * @Author atguigu-mqx
 * @Description TODO
 * @Date 2023/5/12 11:55
 * @Version 1.0
 */
@Service
@Slf4j
public class ItemServiceImpl implements ItemService {

    @Autowired
    private ProductFeignClient productFeignClient;

    @Autowired
    private RedissonClient redissonClient;

    @Autowired
    private ThreadPoolExecutor threadPoolExecutor;

    @Autowired
    private ListFeignClient listFeignClient;



    @Override
    public Map<String, Object> getItem(Long skuId) {
        //  创建一个map 集合
        Map<String, Object> map = new HashMap<>();
        //  判断布隆过滤器中是否存在该商品!
        RBloomFilter<Object> bloomFilter = redissonClient.getBloomFilter(RedisConst.SKU_BLOOM_FILTER);
        if (!bloomFilter.contains(skuId)){
            log.error("该商品不在布隆过滤器中{}",skuId);
            return new HashMap<>();
        }

        //  后面的方法中，有需要当前这对象作为参数传递的！
        CompletableFuture<SkuInfo> skuInfoCompletableFuture = CompletableFuture.supplyAsync(() -> {
            //  获取远程调用的结果
            SkuInfo skuInfo = this.productFeignClient.getSkuInfo(skuId);//  后续会将这个对象放入缓存
            map.put("skuInfo", skuInfo);
            return skuInfo;
        },threadPoolExecutor);

        //  Long category3Id = skuInfo.getCategory3Id();
        //  获取分类数据
        CompletableFuture<Void> categoryCompletableFuture = skuInfoCompletableFuture.thenAcceptAsync(skuInfo -> {
            BaseCategoryView categoryView = this.productFeignClient.getCategoryView(skuInfo.getCategory3Id());
            map.put("categoryView", categoryView);
        },threadPoolExecutor);

        //  获取最新价格
        CompletableFuture<Void> priceCompletableFuture = CompletableFuture.runAsync(() -> {
            BigDecimal skuPrice = this.productFeignClient.getSkuPrice(skuId);
            map.put("price", skuPrice);
        },threadPoolExecutor);

        //  根据spuId 获取海报信息
        CompletableFuture<Void> postCompletableFuture = skuInfoCompletableFuture.thenAcceptAsync(skuInfo -> {
            List<SpuPoster> spuPosterList = this.productFeignClient.findSpuPosterBySpuId(skuInfo.getSpuId());
            map.put("spuPosterList", spuPosterList);
        },threadPoolExecutor);
        //  根据skuId 获取到平台属性：
        CompletableFuture<Void> attrCompletableFuture = CompletableFuture.runAsync(() -> {
            List<BaseAttrInfo> attrList = this.productFeignClient.getAttrList(skuId);
            List<HashMap<String, Object>> mapList = attrList.stream().map(baseAttrInfo -> {
                HashMap<String, Object> hashMap = new HashMap<>();
                hashMap.put("attrName", baseAttrInfo.getAttrName());
                //  在skuId 确定的情况下，一个平台属性名，只能对应一个平台属性值！
                hashMap.put("attrValue", baseAttrInfo.getAttrValueList().get(0).getValueName());
                return hashMap;
            }).collect(Collectors.toList());
            map.put("skuAttrList", mapList);
        },threadPoolExecutor);

        //  销售属性回显锁定
        CompletableFuture<Void> saleCompletableFuture = skuInfoCompletableFuture.thenAcceptAsync(skuInfo -> {
            List<SpuSaleAttr> spuSaleAttrList = productFeignClient.getSpuSaleAttrListCheckBySku(skuId, skuInfo.getSpuId());
            map.put("spuSaleAttrList", spuSaleAttrList);
        },threadPoolExecutor);

        //  获取切换功能数据
        CompletableFuture<Void> jsonCompletableFuture = skuInfoCompletableFuture.thenAcceptAsync(skuInfo -> {
            Map skuValueIdsMap = productFeignClient.getSkuValueIdsMap(skuInfo.getSpuId());
            String mapJson = JSON.toJSONString(skuValueIdsMap);
            System.out.println(mapJson);
            map.put("valuesSkuJson", mapJson);
        },threadPoolExecutor);

        //  开启线程调用热度排名方法.
        CompletableFuture<Void> incrCompletableFuture = CompletableFuture.runAsync(() -> {
            listFeignClient.incrHotScore(skuId);
        },threadPoolExecutor);
        //  多任务组合
        CompletableFuture.allOf(
                skuInfoCompletableFuture,
                categoryCompletableFuture,
                priceCompletableFuture,
                postCompletableFuture,
                saleCompletableFuture,
                attrCompletableFuture,
                jsonCompletableFuture,
                incrCompletableFuture
        ).join();
        //  返回数据
        return map;
    }
}
